package com.deer.wms.base.system.web.zhBusiness.business;

import com.alibaba.fastjson.JSONArray;
import com.deer.wms.base.system.finalUtil.SiteInfo;
import com.deer.wms.base.system.model.bill.BillOutMesLog;
import com.deer.wms.base.system.model.ware.ShelfInfo;
import com.deer.wms.base.system.model.zh.*;
import com.deer.wms.base.system.service.bill.BillOutMesLogService;
import com.deer.wms.base.system.service.zh.QualityService;
import com.deer.wms.base.system.service.zh.ZhMesService;
import com.deer.wms.common.core.controller.BaseController;
import com.deer.wms.common.core.domain.AjaxResult;
import com.deer.wms.common.core.page.TableDataInfo;
import com.deer.wms.common.core.result.CommonCode;
import com.deer.wms.common.core.result.ResultGenerator;
import com.deer.wms.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 李帅辰
 * @date 2021/5/31 9:37
 */
@Controller
@RequestMapping("/Quality")
@Slf4j
public class QualityController extends BaseController {
    @Autowired
    private QualityService qualityService;

    @Autowired
    private ZhMesService zhMesService;

    @Autowired
    private BillOutMesLogService billOutMesLogService;

    private String prefix = "quality";

    /**
     *跳转显示页面
     * @return
     */
    @RequiresPermissions("quality:view")
    @GetMapping
    public String quality()
    {
        return prefix + "/quality";
    }

    /**
     * 导出列表
     */
    //@RequiresPermissions("in:billInMaster:partExcel")
    @PostMapping("/partExcel")
    @ResponseBody
    public AjaxResult partExcel(@RequestParam  String ids) {
        List<QualityDto> byIds=new ArrayList();
        String[] split = ids.split(",");
        for (String s:split
        ) {
            Quality b=new Quality ();
            b.setQualityId(Integer.parseInt(s));
            List<QualityDto> list = qualityService.findQualityList(b);
            for (QualityDto bs:list
            ) {
                byIds.add(bs);
            }

        }
        // List<BillInSapDto> billInSapList = billInSapService.findBillInSapDtoList(billInSapCriteria);
        ExcelUtil<QualityDto> util = new ExcelUtil<QualityDto>(QualityDto.class);
        return util.exportExcel(byIds, "质检数据报表");
    }


    @RequiresPermissions("quality:findList")
    @PostMapping("/findList")
    @ResponseBody
    public TableDataInfo findList(Quality quality) {
        startPage();
        List<QualityDto> list = qualityService.findQualityList(quality);
        return getDataTable(list);
    }

    @RequiresPermissions("quality:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Quality quality) {

        List<QualityDto> list = qualityService.findQualityList(quality);
        ExcelUtil<QualityDto> util = new ExcelUtil<QualityDto>(QualityDto.class);
        return util.exportExcel(list, "质检数据报表");
    }


    /**
     *接受WCS传来的之间数据
     * @author 李帅辰
     * @date 2021/5/31 9:49
     * @return boolean
     */
    @PostMapping("/materialQuality")
    @ResponseBody
    @Transactional
    public  boolean materialQuality(String param){
        Quality quality = JSONArray.parseObject(param, Quality.class);//获取到任务，转换格式
        QualityDto quality1 = qualityService.findQuality(quality);
        if (null != quality1) {
            //新增成功，将质检数据同步MES
            int i = qualityService.saveQuality(quality);
            if (i > 0) {
                //IngotIocus ingotIocus = new IngotIocus(quality.getMaterialId(), SiteInfo.BILLIN_SECOND,"WCS","wcs传递质检的晶棒"+quality.getMaterialId(),"");//晶棒时间点信息录入\
                //此处发送HTTP请求，同步mes
             /*   String s = zhMesService.QualityMes(quality);
                log.info("MES返回值"+s);
                ZhResult zhResult= JSONArray.parseObject(s, ZhResult.class);
                Integer code = zhResult.getCode();
                BillOutMesLog billOutMesLog = new BillOutMesLog();
                billOutMesLog.setCode(code.toString());
                billOutMesLog.setJson(JSONArray.toJSONString(quality));
                billOutMesLog.setOp("NMMaterialInspectData");
                billOutMesLog.setResultJson(s);
                billOutMesLogService.saveBillOutMesLog(billOutMesLog);
                if (code!=0){
                    return false;
                }*/
                return true;
            }
            return false;
        }
        return true;
    }

}
